define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/shop/index' + location.search,
                    add_url: 'shop/shop/add',
                    edit_url: 'shop/shop/edit',
                    del_url: 'shop/shop/del',
                    multi_url: 'shop/shop/multi',
                    import_url: 'shop/shop/import',
                    table: 'shop',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'avatar',
                            title: __('Avatar'),
                            operate: 'LIKE',
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        // {field: 'password', title: __('Password'), operate: 'LIKE'},
                        // {field: 'salt', title: __('Salt'), operate: 'LIKE'},
                        {
                            field: 'address',
                            title: __('Address'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'category_count',
                            title: __('Category Num'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content,
                        },
                        {
                            field: 'goods_count',
                            title: __('Goods Num'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content,
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        //   {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange',
                        //   autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'Class_Manage',
                                    text: __('Class Manage'),
                                    title: __('Class Manage'),
                                    extend: 'data-area=["100%","100%"]',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'shop/shop/class_manage',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                                {
                                    name: 'Goods_Manage',
                                    text: __('Goods Manage'),
                                    title: __('Goods Manage'),
                                    extend: 'data-area=["100%","100%"]',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'shop/shop/goods_manage',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                                {
                                    name: 'Board',
                                    text: __('Board'),
                                    title: __('Board'),
                                    extend: 'data-area=["100%","100%"]',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'shop/board/index',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'shop/shop/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'shop/shop/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'shop/shop/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        class_manage: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/shop/class_manage?shop_id=' + Config.shop_id + '&' + location.search,
                    table: 'goods_category',
                }
            });


            $('.add-category').click(function () {
                var ids = Table.api.selectedids(table);
                if (ids.length <= 0) {
                    return layer.alert('Please select the category you want to add');
                }
                layer.confirm('Are you sure you want to add？', {
                    btn: ['Yes', 'No'] //按钮
                }, function (index) {

                    $.ajax({
                        url: "shop/shop/update_shop_category",
                        type: "post",
                        data: {ids: ids, shop_id: Config.shop_id},
                        success: function (res) {
                            if (res.code == 0) {
                                table.bootstrapTable('refresh');
                                layer.alert(res.msg)

                            } else {
                                layer.alert(res.msg)
                            }
                        }
                    })
                    Layer.close(index);
                }, function () {
                });
            })
            $('.del-category').click(function () {
                var ids = Table.api.selectedids(table);
                if (ids.length <= 0) {
                    return layer.alert('Please select the category you want to add');
                }
                layer.confirm('Are you sure you want to delete？', {
                    btn: ['Yes', 'No'] //按钮
                }, function (index) {

                    $.ajax({
                        url: "shop/shop/delete_shop_category",
                        type: "post",
                        data: {ids: ids, shop_id: Config.shop_id},
                        success: function (res) {
                            if (res.code == 0) {
                                table.bootstrapTable('refresh');
                                layer.alert(res.msg)

                            } else {
                                layer.alert(res.msg)
                            }
                        }
                    })
                    Layer.close(index);
                }, function () {
                });
            })

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        //      {field: 'pid', title: __('Pid')},
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'avatar',
                            title: __('Avatar'),
                            operate: 'LIKE',
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        // {field: 'weigh', title: __('Weigh'), operate: false},
                        {
                            field: 'have', title: __('Whether to choose or not'),
                            searchList: {100: "Not owned", 200: "Already have"},
                            formatter: Table.api.formatter.status
                        },
                        // {
                        //     field: 'status',
                        //     title: __('Status'),
                        //     searchList: {"200": __('Normal'), "100": __('Hidden')},
                        //     formatter: Table.api.formatter.status
                        // },
                        // {
                        //     field: 'createtime',
                        //     title: __('Createtime'),
                        //     operate: 'RANGE',
                        //     addclass: 'datetimerange',
                        //     autocomplete: false,
                        //     formatter: Table.api.formatter.datetime
                        // },
                        //  {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange',
                        //  autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {
                        //     field: 'operate',
                        //     title: __('Operate'),
                        //     table: table,
                        //     events: Table.api.events.operate,
                        //     formatter: Table.api.formatter.operate
                        // }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        goods_manage: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/shop/goods_manage?shop_id='+Config.shop_id+'&' + location.search,
                    table: 'goods',
                }
            });
            $(".btn-add").data("area", ["100%", "100%"]);

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });



            $('.add-goods').click(function () {
                var ids = Table.api.selectedids(table);
                if (ids.length <= 0) {
                    return layer.alert('Please select the goods you want to add');
                }
                layer.confirm('Are you sure you want to add？', {
                    btn: ['Yes', 'No'] //按钮
                }, function (index) {

                    $.ajax({
                        url: "shop/shop/add_shop_goods",
                        type: "post",
                        data: {ids: ids, shop_id: Config.shop_id},
                        success: function (res) {
                            if (res.code == 0) {
                                table.bootstrapTable('refresh');
                                layer.alert(res.msg)

                            } else {
                                layer.alert(res.msg)
                            }
                        }
                    })
                    Layer.close(index);
                }, function () {
                });
            })
            $('.del-goods').click(function () {
                var ids = Table.api.selectedids(table);
                if (ids.length <= 0) {
                    return layer.alert('Please select the goods you want to add');
                }
                layer.confirm('Are you sure you want to delete？', {
                    btn: ['Yes', 'No'] //按钮
                }, function (index) {

                    $.ajax({
                        url: "shop/shop/delete_shop_goods",
                        type: "post",
                        data: {ids: ids, shop_id: Config.shop_id},
                        success: function (res) {
                            if (res.code == 0) {
                                table.bootstrapTable('refresh');
                                layer.alert(res.msg)

                            } else {
                                layer.alert(res.msg)
                            }
                        }
                    })
                    Layer.close(index);
                }, function () {
                });
            })


            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Cn name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'en_name', title: __('En name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},

                        {field: 'category_id', title: __('Category'),addClass:'selectpage',
                            extend:'data-source="goods/goods_category/index"',
                            formatter: function (value,row) {return row.category_name}},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {
                            field: 'have', title: __('Whether to choose or not'),
                            searchList: {100: "Not owned", 200: "Already have"},
                            formatter: Table.api.formatter.status
                        },
                    ]
                ]
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
